package com.gofunbuy.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

import com.gofunbuy.core.CouponEntity;
import com.gofunbuy.core.JpEntity;
import com.gofunbuy.core.ProductAdEntity;
import com.gofunbuy.core.ProductClassEntity;
import com.gofunbuy.core.ProductEntity;
import com.gofunbuy.core.ReceiptEntity;
import com.gofunbuy.http.HttpClientImpl;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface.ProtocalStatics;
import com.gofunbuy.util.GoFunBuyUtils.IntegrationXML;
import com.gofunbuy.util.GoFunBuyUtils.LoginLevelXML;
import com.gofunbuy.util.GoFunBuyUtils.LoginXML;
import com.gofunbuy.util.GoFunBuyUtils.PayHistoryXML;
import com.gofunbuy.util.GoFunBuyUtils.RegistXML;

public class XmlUtil {

	/***
	 * 获取积分记录
	 * 
	 * @param xmlStr
	 *            网络返回的数据字符串
	 * @return 积分记录
	 */
	public static List<JpEntity> getJpList(InputStream inStream) {
		/*
		 * <XML> <p source="来源" jp="150" date="12/05/2013" shop="eye4gift"
		 * shopid="124123" /> </XML>
		 */
		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			JpEntity currentJp = null;
			List<JpEntity> jps = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					jps = new ArrayList<JpEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					if (name.equalsIgnoreCase("p")) {
						currentJp = new JpEntity();
						currentJp.setSource(parser.getAttributeValue(null,
								"source"));
						currentJp.setJp(parser.getAttributeValue(null, "jp"));
						currentJp.setDate(parser
								.getAttributeValue(null, "date"));
						currentJp.setShop(parser
								.getAttributeValue(null, "shop"));
						currentJp.setShopid(parser.getAttributeValue(null,
								"shopid"));

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("p")
							&& currentJp != null) {
						jps.add(currentJp);
						currentJp = null;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return jps;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<CouponEntity> getCouponList(InputStream inStream) {
		/*
		 * <XML> <XML> <sort name="飲食優惠" id=""/> <sort name="包場優惠" id=""> <p
		 * id="16" name="會員特別包場優惠" price="0" dep=" 所有會員9折包場優惠" have="1" img=
		 * "http://www.eye4gift.com/gofun/upload/news/500/201308271844148606.png"
		 * /> </sort> <sort name="活動優惠" id=""> <p id="17" name="Go Inside 夢想活動"
		 * price="0" dep="" have="1" img=
		 * "http://www.eye4gift.com/gofun/upload/news/500/201308271905404994.png"
		 * /> </sort> <sort name="生日優惠" id=""> <p id="15" name="會員生日優惠"
		 * price="0" dep="" have="1" img=""/> </sort> </XML>
		 * 
		 * </XML>
		 */

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			CouponEntity currentCoupon = null;
			List<CouponEntity> coupons = null;

			String currentTypeId = null;
			String currentTypeName = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					coupons = new ArrayList<CouponEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equals("sort")) {
						currentTypeId = parser.getAttributeValue(null, "id");
						currentTypeName = parser
								.getAttributeValue(null, "name");

					} else if (name.equalsIgnoreCase("p")) {

						currentCoupon = new CouponEntity();

						currentCoupon.setTypeName(currentTypeName);
						currentCoupon.setTypeId(currentTypeId);

						currentCoupon.setId(parser
								.getAttributeValue(null, "id"));
						currentCoupon.setName(parser.getAttributeValue(null,
								"name"));
						currentCoupon.setPrice(parser.getAttributeValue(null,
								"price"));
						currentCoupon.setHave(parser.getAttributeValue(null,
								"have"));
						currentCoupon.setDep(parser.getAttributeValue(null,
								"dep"));
						currentCoupon.setImgPath(parser.getAttributeValue(null,
								"img"));

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("p")
							&& currentCoupon != null) {
						coupons.add(currentCoupon);
						currentCoupon = null;

					} else if (parser.getName().equalsIgnoreCase("sort")) {
						currentTypeId = null;
						currentTypeName = null;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return coupons;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Map<String,String> getOrderReturn(InputStream inStream) {

		/*<XML>
    	<re code="1" title="消费成功或别的标题" message="谁谁谁用多少积分买了什么东西或谁积分不足，购买失败什么的" />
		</XML>*/

		XmlPullParser parser = Xml.newPullParser();
		Map<String,String> orderReurn = new HashMap<String,String>();
		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equals("re")) {	
						String code = parser.getAttributeValue(null,
								"code");
						String title = parser.getAttributeValue(null,
								"title");	
						String message = parser.getAttributeValue(null,
								"message");	
						orderReurn.put("code", code);
						orderReurn.put("title", title);
						orderReurn.put("message", message);
					} 
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("re")) {
					
					}
					break;
				}
				
				eventType = parser.next();				
			}

			inStream.close();
			return orderReurn;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static List<ProductClassEntity> getProductClassList(InputStream inStream) {

		/*<XML>
		<sort name="Go Inside 夢想隨行杯" id="52"/>
		</XML>*/

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			ProductClassEntity currentProduct = null;
			List<ProductClassEntity> products = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					products = new ArrayList<ProductClassEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equals("sort")) {
						
						currentProduct = new ProductClassEntity();
						currentProduct.setId( parser.getAttributeValue(null, "id"));
						currentProduct.setName(parser.getAttributeValue(null, "name"));

					} 
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("sort")) {
						products.add(currentProduct);
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();			
			return products;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static List<ProductEntity> getProductList(InputStream inStream) {
		/*
		 <XML>
		<sort name="Go Inside 夢想隨行杯" id="52">
		<p id="14" name="Flying to our dreams" price="98" jp="0" have="yes" donation="1.96" img="http://www.eye4gift.com/gofun/upload/news/500/201308271812253574.png"/>
		<p id="13" name="Go inside's hearts_modern" price="98" jp="0" have="yes" donation="1.96" img="http://www.eye4gift.com/gofun/upload/news/500/201308271810274150.png"/>
		<p id="7" name="Go Inside Your Heart" price="98" jp="0" have="yes" donation="1.96" img="http://www.eye4gift.com/gofun/upload/news/500/201308271706286656.png"/>
		</sort>
		</XML>
		 */

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			ProductEntity currentProduct = null;
			List<ProductEntity> products = null;

			String classId = null;
			String className = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					products = new ArrayList<ProductEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equals("sort")) {
						classId = parser.getAttributeValue(null, "id");
						className = parser.getAttributeValue(null, "name");

					} else if (name.equalsIgnoreCase("p")) {

						currentProduct = new ProductEntity();

						currentProduct.setClassName(className);
						currentProduct.setClassId(classId);

						currentProduct.setId(parser.getAttributeValue(null,
								"id"));
						currentProduct.setName(parser.getAttributeValue(null,
								"name"));
						currentProduct.setPrice(parser.getAttributeValue(null,
								"price"));
						currentProduct.setHave(parser.getAttributeValue(null,
								"have"));
						currentProduct.setJp(parser.getAttributeValue(null,
								"jp"));
						currentProduct.setImgUri(parser.getAttributeValue(null,
								"img"));
						currentProduct.setDonation(parser.getAttributeValue(null,
								"donation"));

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("p")
							&& currentProduct != null) {
						products.add(currentProduct);
						currentProduct = null;

					} else if (parser.getName().equalsIgnoreCase("sort")) {
						classId = null;
						className = null;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return products;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<ProductAdEntity> getProductAdList(InputStream inStream) {
		/*
		 * <XML> <ad id="22" img="完整路径/xxxx.jpg"/> </XML>
		 */

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			ProductAdEntity currentAd = null;
			List<ProductAdEntity> ads = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					ads = new ArrayList<ProductAdEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equalsIgnoreCase("ad")) {

						currentAd = new ProductAdEntity();

						currentAd.setId(parser.getAttributeValue(null, "id"));
						currentAd.setImgPath(parser.getAttributeValue(null,
								"img"));

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("ad")
							&& currentAd != null) {
						ads.add(currentAd);
						currentAd = null;

					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return ads;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<ReceiptEntity> getReceiptList(InputStream inStream) {
		/*
		 * <XML> <r id="12442" name="Go Inside Cafe" price="80" donation="15"
		 * date="15/7/2013  7:25PM" /> </XML>
		 */

		XmlPullParser parser = Xml.newPullParser();
		try {
			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			ReceiptEntity currentAd = null;
			List<ReceiptEntity> ads = null;

			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					ads = new ArrayList<ReceiptEntity>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();

					if (name.equalsIgnoreCase("r")) {

						currentAd = new ReceiptEntity();

						currentAd.setId(parser.getAttributeValue(null, "id"));
						currentAd.setName(parser
								.getAttributeValue(null, "name"));
						currentAd.setPrice(parser.getAttributeValue(null,
								"price"));
						currentAd.setDonation(parser.getAttributeValue(null,
								"donation"));
						currentAd.setDate(parser
								.getAttributeValue(null, "date"));

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("r")
							&& currentAd != null) {
						ads.add(currentAd);
						currentAd = null;

					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return ads;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * 获取登陆反馈信息
	 * 
	 * @param xmlStr
	 *            网络返回的数据字符串
	 * @return 积分记录
	 */
	public static LoginXML getLoginItem(InputStream inStream) {
		/*
		 * <XML> <p source="来源" jp="150" date="12/05/2013" shop="eye4gift"
		 * shopid="124123" /> </XML>
		 */

		XmlPullParser parser = Xml.newPullParser();
		LoginXML jps = null;
		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
//			Log.v("getLoginItem",
//					"result:" + GoFunBuyUtils.convertStreamToString(inStream));
			ArrayList<LoginLevelXML> levels = new ArrayList<LoginLevelXML>();
			LoginLevelXML level = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					jps = new LoginXML();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					Log.v("111", "name:" + name);
					if (name.equalsIgnoreCase("login")) {
						// jps = new LoginXML();
						jps.id = parser.getAttributeValue(null,
								ProtocalStatics.ID);
						Log.v("111", "id:" + jps.id);
						jps.name = parser.getAttributeValue(null,
								ProtocalStatics.NAME);
						jps.tel = parser.getAttributeValue(null,
								ProtocalStatics.TEL);
						jps.email = parser.getAttributeValue(null,
								ProtocalStatics.EMAIL);
						jps.level = parser.getAttributeValue(null,
								ProtocalStatics.LEVEL);

					} else if (name.equalsIgnoreCase("l")) {
						level = new LoginLevelXML();
						level.level = parser.getAttributeValue(null,
								ProtocalStatics.LEVEL);
						level.name = parser.getAttributeValue(null,
								ProtocalStatics.NAME);
						level.dep = parser.getAttributeValue(null,
								ProtocalStatics.DEP);
						levels.add(level);

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("l") && jps != null) {
						jps.levels = levels;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return jps;

		} catch (Exception e) {
			e.printStackTrace();
			return jps;
		}
	}

	/***
	 * 获取注册反馈信息
	 * 
	 * @param xmlStr
	 *            网络返回的数据字符串
	 * @return 积分记录
	 */
	public static RegistXML getRegistItem(InputStream inStream) {
		/*
		 * <XML> <p source="来源" jp="150" date="12/05/2013" shop="eye4gift"
		 * shopid="124123" /> </XML>
		 */

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			// Log.v("getRegistItem",
			// "result:" + GoFunBuyUtils.convertStreamToString(inStream));
			RegistXML jps = null;
			ArrayList<LoginLevelXML> levels = new ArrayList<LoginLevelXML>();
			LoginLevelXML level = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					jps = new RegistXML();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					Log.v("111", "name:" + name);
					if (name.equalsIgnoreCase("login")) {
						// jps = new LoginXML();
						jps.code = parser.getAttributeValue(null,
								ProtocalStatics.CODE);
						jps.id = parser.getAttributeValue(null,
								ProtocalStatics.ID);
						jps.name = parser.getAttributeValue(null,
								ProtocalStatics.NAME);
						jps.tel = parser.getAttributeValue(null,
								ProtocalStatics.TEL);
						jps.email = parser.getAttributeValue(null,
								ProtocalStatics.EMAIL);
						jps.level = parser.getAttributeValue(null,
								ProtocalStatics.LEVEL);

					} else if (name.equalsIgnoreCase("l")) {
						level = new LoginLevelXML();
						level.level = parser.getAttributeValue(null,
								ProtocalStatics.LEVEL);
						level.name = parser.getAttributeValue(null,
								ProtocalStatics.NAME);
						level.dep = parser.getAttributeValue(null,
								ProtocalStatics.DEP);
						levels.add(level);

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("l") && jps != null) {
						jps.levels = levels;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return jps;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * 获取注册反馈信息
	 * 
	 * @param xmlStr
	 *            网络返回的数据字符串
	 * @return 积分记录
	 */
	public static IntegrationXML getDonationItem(InputStream inStream) {

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			// Log.v("getDonationItem",
			// "result:" + GoFunBuyUtils.convertStreamToString(inStream));
			IntegrationXML jps = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					jps = new IntegrationXML();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					Log.v("111", "name:" + name);
					if (name.equalsIgnoreCase("info")) {
						// jps = new LoginXML();
						jps.jp = parser.getAttributeValue(null,
								ProtocalStatics.JP);
						jps.donation = parser.getAttributeValue(null,
								ProtocalStatics.DONATION);

					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("info")
							&& jps != null) {
						// jps.levels = levels;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return jps;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * 获取注册反馈信息
	 * 
	 * @param xmlStr
	 *            网络返回的数据字符串
	 * @return 积分记录
	 */
	public static ArrayList<PayHistoryXML> getReceiptsList(InputStream inStream) {

		XmlPullParser parser = Xml.newPullParser();

		try {

			parser.setInput(inStream, "UTF-8");
			int eventType = parser.getEventType();
			// Log.v("getDonationItem",
			// "result:" + GoFunBuyUtils.convertStreamToString(inStream));
			ArrayList<PayHistoryXML> jps = null;
			PayHistoryXML item = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					jps = new ArrayList<PayHistoryXML>();
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					Log.v("111", "name:" + name);
					if (name.equalsIgnoreCase("r")) {
						item = new PayHistoryXML();
						item.id = parser.getAttributeValue(null,
								ProtocalStatics.ID);
						item.cost = parser.getAttributeValue(null,
								ProtocalStatics.PRICE);
						item.donation = parser.getAttributeValue(null,
								ProtocalStatics.DONATION);
						item.time = parser.getAttributeValue(null,
								ProtocalStatics.DATE);
						item.goods = parser.getAttributeValue(null,
								ProtocalStatics.NAME);
					}
					break;

				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("r") && jps != null) {
						jps.add(item);
						item = null;
					}
					break;
				}
				eventType = parser.next();
			}

			inStream.close();
			return jps;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
